//
//  SportsMusicViewController.m
//  HappySporting
//
//  Created by 康康 on 16/6/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SportsMusicViewController.h"
#import "SportMusicListViewController.h"
#import "MyMusicViewController.h"
@interface SportsMusicViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *myScrollView;
@property (nonatomic, retain) NSArray *buttonArray;
@property (nonatomic, retain) SportMusicListViewController *listVC;
@property (nonatomic, retain) MyMusicViewController *musicVC;

@end

@implementation SportsMusicViewController

- (void)dealloc
{
    [_myScrollView release];
    [_buttonArray release];
    [_listVC release];
    [_musicVC release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   // self.view.backgroundColor = [UIColor yellowColor];
    
   // self.edgesForExtendedLayout = 0;
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.myScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myScrollView];
    [_myScrollView release];
    self.myScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.bounces = NO;
    self.myScrollView.delegate = self;

    
    self.musicVC = [[MyMusicViewController alloc] init];
    [self addChildViewController:self.musicVC];
    self.musicVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.myScrollView addSubview:self.musicVC.view];
    
    [_musicVC release];
    
    self.listVC = [[SportMusicListViewController alloc] init];
    [self addChildViewController:self.listVC];
    self.listVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.myScrollView addSubview:self.listVC.view];
    [_listVC release];

    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH / 2, 34)];
    //titleView.backgroundColor = [UIColor blackColor];
    self.navigationItem.titleView = titleView;
    
    
    UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];
    listButton.frame = CGRectMake(0, 5, SCREEN_WIDTH/4, 34);
    [titleView addSubview:listButton];
    
    [listButton setTitle:@"运动歌单" forState:UIControlStateNormal];
    [listButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [listButton addTarget:self action:@selector(listAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *musicButton = [UIButton buttonWithType:UIButtonTypeCustom];
    musicButton.frame = CGRectMake(SCREEN_WIDTH / 4, 5, SCREEN_WIDTH / 4, 34);
    [titleView addSubview:musicButton];
    
    [musicButton setTitle:@"我的音乐" forState:UIControlStateNormal];
    [musicButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [musicButton addTarget:self action:@selector(musicAction:) forControlEvents:UIControlEventTouchUpInside];
    self.buttonArray = [NSArray arrayWithObjects:listButton,musicButton, nil];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchAction)];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    for (UIButton *button in self.buttonArray) {
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }
   
    UIButton *button = [self.buttonArray objectAtIndex:scrollView.contentOffset.x / SCREEN_WIDTH];
    
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    
}

- (void)listAction:(UIButton *)button
{
    [self.myScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    for (UIButton *button in self.buttonArray) {
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    
}




- (void)musicAction:(UIButton *)button
{
    [self.myScrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
    for (UIButton *button in self.buttonArray) {
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    
}












@end
